using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace Sio.Mdm.Utils
{
    /// <summary>
    /// Some helper functions for parsing config nodes
    /// </summary>
    public static class ConfigHelper
    {
        #region Methods

        #region Public

        #region GetNodeByName
        /// <summary>
        /// Get child node by name
        /// </summary>
        /// <param name="doc"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static XmlNode GetNodeByName(XmlDocument doc, string name)
        {
            foreach (XmlNode node in doc.ChildNodes)
            {
                if (String.Compare(node.Name, name, true) == 0)
                {
                    return node;
                }
            }

            return null;
        }
        /// <summary>
        /// Get child node by name
        /// </summary>
        /// <param name="parentNode"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static XmlNode GetNodeByName(XmlNode parentNode, string name)
        {
            foreach (XmlNode node in parentNode.ChildNodes)
            {
                if (String.Compare(node.Name, name, true) == 0)
                {
                    return node;
                }
            }

            return null;
        }
        #endregion

        #region GetAttributeValueByName
        /// <summary>
        /// Get attribute value and make sure attribute exist
        /// </summary>
        /// <param name="node"></param>
        /// <param name="attributeName"></param>
        /// <returns></returns>
        public static string GetAttributeValueByName(XmlNode node, string attributeName)
        {
            return node.Attributes[attributeName] == null ?
                String.Empty : node.Attributes[attributeName].Value;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="node"></param>
        /// <param name="attributeName"></param>
        /// <returns></returns>
        public static int GetAttributeValueByNameInt(XmlNode node, string attributeName)
        {
            return StringParser.GetInteger(node.Attributes[attributeName].Value, Int32.MinValue);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="node"></param>
        /// <param name="attributeName"></param>
        /// <returns></returns>
        public static DateTime GetAttributeValueByNameDateTime(XmlNode node, string attributeName)
        {
            return StringParser.GetDateTime(node.Attributes[attributeName].Value, DateTime.MinValue);
        }
        #endregion

        #endregion
        #endregion
    }
}
